﻿#region File Description
/****************************************************************************
*                                                                           *
* Game Manager.                                                             *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2013).                  *
*                                                                           *
****************************************************************************/
#endregion
namespace Silverlight3D
{
    using System.Windows;
    using System.Collections.Generic;

    public class _2DHelper
    {
        /// <summary>
        /// Tests a 2D point to see if it lies within a 2D polygon.
        /// </summary>
        /// <param name="polygonPoints">List of 2D points - the vertices of the 2D polygon</param>
        /// <param name="point">Point to test against polygon</param>
        /// <returns>Whether the point is within the polygon</returns>
        /// <remarks>See http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html </remarks>
        public static bool PointIntersectsPolygon(Point point, IList<Point> polygonPoints)
        {
            int i;
            int j;
            bool c = false;

            for (i = 0, j = polygonPoints.Count - 1; i < polygonPoints.Count; j = i++)
            {
                if (((polygonPoints[i].Y > point.Y) != (polygonPoints[j].Y > point.Y)) && (point.X < (polygonPoints[j].X - polygonPoints[i].X) * (point.Y - polygonPoints[i].Y) / (polygonPoints[j].Y - polygonPoints[i].Y) + polygonPoints[i].X))
                {
                    c = !c;
                }
            }

            return c;
        }
    }
}